package com.ml.mall.service.asyn;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ml.mall.bean.req.company.CompanyDto;
import com.ml.mall.bean.req.product.SysProductNewReq;
import com.ml.mall.bean.res.company.CompanyQueryInfoRes;
import com.ml.mall.consts.MailContentConsts;
import com.ml.mall.entity.category.SysACategory;
import com.ml.mall.entity.company.SysCompany;
import com.ml.mall.entity.product.SysProduct;
import com.ml.mall.entity.sys.SysUser;
import com.ml.mall.model.MailMessage;
import com.ml.mall.service.category.SysACategoryService;
import com.ml.mall.service.company.SysCompanyService;
import com.ml.mall.service.mail.MailService;
import com.ml.mall.service.notifation.SysANotifationService;
import com.ml.mall.service.sys.SysUserService;
import com.ml.mall.utils.MailUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

/**
 * Created by kyyang on 2021/7/27 23:25
 */
@Component
public class ProductMailServiceImplAsyn {

    @Autowired
    private MailService mailService;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysCompanyService sysCompanyService;

    @Autowired
    private SysACategoryService sysACategoryService;

    @Autowired
    private SysANotifationService sysANotifationService;

    @Value("${dns.front_url}")
    private String front_url;


    /**
     * 拒绝产品发送邮件，通知卖家
     * @param sysProduct
     */
    @Async
    public void sysProductRejectedMailAsyn(SysProduct sysProduct){
        //获取卖家
        SysCompany supplier = sysCompanyService.queryByCompanyId(sysProduct.getSupplierId());

        MailMessage mailMessage = mailService.sendForProductRejected(supplier.getEmailAddress());

        //内容替换
        mailMessage.contentReplace("reject_reason", sysProduct.getRefusReasion());
        mailMessage.contentReplace("product_name", sysProduct.getProductName());
        mailMessage.contentReplace("product_url", front_url + MailContentConsts.PRODUCT_PAGE_URL);
        mailMessage.contentReplace("contact_no", MailContentConsts.CONCAT);
        mailMessage.contentReplace("general_email", MailContentConsts.GENERAL_EMAIL);
        mailMessage.contentReplace("mainPicUrl", MailContentConsts.MAIN_PIC_URL);

        //通知卖家
        sysANotifationService.notifyCompany(mailMessage, supplier.getCompanyId());

        //发送邮件给卖家
        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
        wrapper.in("role_id", Arrays.asList(new Integer[]{8, 9, 10, 11}))
               .eq("company_id", supplier.getCompanyId());
        List<SysUser> accs = sysUserService.list(wrapper);
        String content=mailMessage.getContent();
        if(accs!=null&&accs.size()>0){
            for(SysUser u:accs){
                mailMessage.setReceiverEmail(u.getEmail());
                mailMessage.setContent(mailMessage.getContent().replace("supplier_name",u.getName()));
                MailUtils.doSendEmail1(mailMessage);
                mailMessage.setContent(content);
            }
        }
    }

    /**
     * @description: 创建产品发送邮件 通知管理员
     * @author kyyang
     * @date 2021/7/29 23:04
     * @version 1.0
     */
    @Async
    public void sysProductNewMailAsyn(SysProductNewReq sysProductNewReq) {

        MailMessage mailMessage = mailService.sendForNewProductUploaded("");
        mailMessage.setContent(mailMessage.getContent().replaceAll("mainPicUrl", MailContentConsts.MAIN_PIC_URL));
        mailMessage.setContent(mailMessage.getContent().replaceAll("general_email",MailContentConsts.GENERAL_EMAIL));
        mailMessage.setContent(mailMessage.getContent().replaceAll("system_url",MailContentConsts.SYSTEM_URL));
        mailMessage.setContent(mailMessage.getContent().replaceAll("contact_no",MailContentConsts.CONCAT));

        CompanyDto dto = new CompanyDto();
        dto.setCompanyId(sysProductNewReq.getSupplierId());
        CompanyQueryInfoRes companyQueryInfoRes = sysCompanyService.queryOneCompany(dto);
        if(companyQueryInfoRes==null){
            return;
        }
        QueryWrapper<SysACategory> query = new QueryWrapper<>();
        query.eq("category_id",sysProductNewReq.getCategoryId());
        SysACategory one = sysACategoryService.getOne(query);
        String category=null;
        if(one != null){
            category=one.getCategoryName();
            if(one.getRootCategoryId() != null){
                QueryWrapper<SysACategory> query2 = new QueryWrapper<>();
                query2.eq("category_id",one.getRootCategoryId());
                category=sysACategoryService.getOne(query2).getCategoryName();
            }
        }
        mailMessage.setContent(mailMessage.getContent().replace("company_name",companyQueryInfoRes.getCompanyName()));
        mailMessage.setContent(mailMessage.getContent().replace("product_name",sysProductNewReq.getProductName()));
        mailMessage.setContent(mailMessage.getContent().replace("CATE_GORY",category==null?"":category));
        mailMessage.setContent(mailMessage.getContent().replace("product_description",sysProductNewReq.getDescription()));
        mailMessage.setContent(mailMessage.getContent().replace("product_brand",sysProductNewReq.getBrand()));
        mailMessage.setContent(mailMessage.getContent().replace("SUB_CATEGORY",one==null?"":one.getCategoryName()));


        if(mailMessage!=null){
            //通知管理员
            sysANotifationService.notifyAdmin(mailMessage);
            //发送邮件
            QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
            wrapper.in("role_id", Arrays.asList(new Integer[]{1,2,4,5}));
            List<SysUser> list = sysUserService.list(wrapper);
            String content=mailMessage.getContent();
            if(list!=null&&list.size()>0){
                for(SysUser u:list){
                    mailMessage.setReceiverEmail(u.getEmail());
                    mailMessage.setContent(mailMessage.getContent().replace("admin_name",u.getName()));
                    MailUtils.doSendEmail1(mailMessage);
                    mailMessage.setContent(content);
                }
            }

        }

    }
}
